import { marked } from "marked";
import DOMPurify from "dompurify";
// import canvasSize from "canvas-size";
// @ts-ignore
import html2pdf from "html2pdf.js";
// import * as FileSaver from "file-saver";

const mdDiv = document.querySelector("#markdown")!;
const saveBtn = document.querySelector("#saveBtn") as HTMLButtonElement;
const file = document.querySelector("#file") as HTMLInputElement;
let name = "download";
file.onchange = function (_result: any) {
  const reader = new FileReader();
  reader.readAsText(file.files![0]);
  reader.onload = function (event: any) {
    const text = event.target.result;
    mdDiv.innerHTML = DOMPurify.sanitize(marked.parse(text) as string, {
      ADD_ATTR: ["target"],
    });

    saveBtn.disabled = false;
  };

  name = file.files![0].name;
};

saveBtn.onclick = function () {
  html2pdf(mdDiv, {
    filename: name + ".pdf",
    image: { type: "jpeg", quality: 0.98 },
    html2canvas: { dpi: 192, letterRendering: true },
    jsPDF: { unit: "in", format: "letter", orientation: "portrait" },
  });
};
